package wiki.generic.concreteutilities;

import java.util.StringTokenizer;

import wiki.generic.utilities.ExtensionController;
import wiki.generic.utilities.FormatUnsupportedException;

/**
 * 
 * @author <p><strong>JwTeam</strong></p> 
 *
 */

public class ConcreteExtensionController implements ExtensionController {

	private String type;
	private static final String[] AUDIO_EXTENSIONS ={"MP3","Ogg", "WAV"};;
	private static final String[] IMG_EXTENSIONS={ "GIF", "JPEG", "JPG", "PNG"};
	private String extension;	
	
	@Override
	public String getFileType(String path) throws FormatUnsupportedException{
		type= null;
		String extension=getExtension(path);
		if (extension==null) {
			throw new FormatUnsupportedException("Non riconosco il formato");
		}
		
		for (int i = 0; i < AUDIO_EXTENSIONS.length; i++) {
			if (extension.equalsIgnoreCase(AUDIO_EXTENSIONS[i])) {
				type="audio";
			}
		}
		for (int i = 0; i < IMG_EXTENSIONS.length; i++) {
			if (extension.equalsIgnoreCase(IMG_EXTENSIONS[i])) {
				type="img";
			}
		}
		
		if(type==null){
			throw new FormatUnsupportedException("Formato non supportato");
		}
		
		
		return type;
		
	}

	@Override
	public String getExtension(String path){
		StringTokenizer tok = new StringTokenizer(path,".");
		extension=tok.nextToken();
		
		while (tok.hasMoreTokens()) {
			
			extension=tok.nextToken();
			
		}
		return extension;
	}
	
	
}
